const mysql = require('mysql');

module.exports = (sql) => {
    //1.创建连接对象
    let connection = mysql.createConnection({
        host:'localhost',
        user:'root',
        password:'root',
        port:3306,
        database:'0801b_userlist'  //数据库
    })

    //2.连接数据库
    connection.connect((error) => {
        if(error){
            console.log("连接数据库失败")
        }else{
            console.log("连接数据库成功")
        }
    })

    //3.和数据库交互
    //注：和数据库交互的过程是异步的
    return new Promise((resolve,reject) => {
        //从表里做增删改查
        connection.query(sql,(error,result) => {
            if(error){
                //有错误
                reject(error);
            }else{
                resolve(result);
            }
            connection.end();
        })
    })
}